import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/Login";
import Main from "@/views/Main";
import {getLoginFlag} from "@/util/auth";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/",
    name: "Main",
    component: Main,
    // 把其他的组件作为 Main 的二级路由
    children:[
      {
        path: "",
        name: "Home",
        component: ()=>import(/* webpackChunkName: "Home" */'@/views/Home'),
      },
      {
        path: "user",
        name: "UserList",
        component: ()=>import(/* webpackChunkName: "User" */'@/views/User/index'),
      },
      {
        path: "appinfo",
        name: "AppInfo",
        component: ()=>import(/* webpackChunkName: "AppInfo" */'@/views/AppInfo/index'),
      }
    ]
  },
  // {
  //   path: "/",
  //   name: "Home",
  //   component: Home,
  // },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
];

const router = new VueRouter({
  routes,
});

// 全局的导航守卫
router.beforeEach((to,from,next)=>{
  if(to.name === "Login"){
    next();
    return;
  }
  let isLogin = getLoginFlag();
  if(isLogin){
    next();
    return;
  }

  next("/login");

});

export default router;
